cc.Class({

    extends: cc.Component,
    properties: {
    },
    onLoad() {
        this.initView();
        this.initData();
        this.initTestData();

    },

    /**
     * 初始化界面
     */
    initView() {
        this.initComponent();
    },
    /**
     * 初始化数据
     */
    initData() {
        window.GameViewData.sysTokenBtnArr = new Array();
        window.GameViewData.sysTokenBtnArr.push(window.GameViewData.advanceTokenItem);
        window.GameViewData.sysTokenBtnArr.push(window.GameViewData.pickTokenItem);
        window.GameViewData.sysTokenBtnArr.push(window.GameViewData.turnLeftTokenItem);
        window.GameViewData.sysTokenBtnArr.push(window.GameViewData.turnRightTokenItem);
        window.GameViewData.sysTokenBtnArr.push(window.GameViewData.leapTokenItem);
        window.GameViewData.sysTokenBtnArr.push(window.GameViewData.func1TokenItem);

        window.GameViewData.stageArr = new Array();
        window.StageManager.createTestData();

        window.GameViewData.curStageIndex = 0;
        window.StageManager.loadStage(0);
    },
    /**
     * 生成测试数据
     */
    initTestData() {
    },
    initComponent: function () {
        window.GameViewData.gameView = this;

        window.GameViewData.lbResult = cc.find("lbResult", this.node).getComponent(cc.Label);
        this.button1 = cc.find("button1", this.node).getComponent(cc.Button);
        this.button2 = cc.find("button2", this.node).getComponent(cc.Button);
        this.button3 = cc.find("button3", this.node).getComponent(cc.Button);

        window.GameViewData.advanceTokenItem = cc.find("itemLayout/advanceTokenItem", this.node).getComponent("TokenItemNode");
        window.GameViewData.pickTokenItem = cc.find("itemLayout/pickTokenItem", this.node).getComponent("TokenItemNode");
        window.GameViewData.pickTokenItem.type = window.GameEnum.TokenTypeEnum.TOKEN_PICK;
        window.GameViewData.turnLeftTokenItem = cc.find("itemLayout/turnLeftTokenItem", this.node).getComponent("TokenItemNode");
        window.GameViewData.turnLeftTokenItem.type = window.GameEnum.TokenTypeEnum.TOKEN_TURN_LEFT;
        window.GameViewData.turnRightTokenItem = cc.find("itemLayout/turnRightTokenItem", this.node).getComponent("TokenItemNode");
        window.GameViewData.turnRightTokenItem.type = window.GameEnum.TokenTypeEnum.TOKEN_TURN_RIGHT;
        window.GameViewData.leapTokenItem = cc.find("itemLayout/leapTokenItem", this.node).getComponent("TokenItemNode");
        window.GameViewData.leapTokenItem.type = window.GameEnum.TokenTypeEnum.TOKEN_LEAP;
        window.GameViewData.func1TokenItem = cc.find("itemLayout/func1TokenItem", this.node).getComponent("TokenItemNode");
        window.GameViewData.func1TokenItem.type = window.GameEnum.TokenTypeEnum.TOKEN_FUNC;
        window.GameViewData.func2TokenItem = cc.find("itemLayout/func2TokenItem", this.node).getComponent("TokenItemNode");
        window.GameViewData.func2TokenItem.type = window.GameEnum.TokenTypeEnum.TOKEN_FUNC2;

        // 动作区域
        window.GameViewData.tokenLayout = cc.find("tokenLayout", this.node).getComponent("cc.Layout");
        window.GameViewData.func1Layout = cc.find("func1Layout", this.node).getComponent("cc.Layout");
        window.GameViewData.func2Layout = cc.find("func2Layout", this.node).getComponent("cc.Layout");

        // 地图区域
        window.GameViewData.mapLayout = cc.find("mapFrame/mapLayout", this.node).getComponent("cc.Layout");
        window.GameViewData.landNode = cc.find("mapFrame/landNode", this.node).getComponent("LandNode");
        window.GameViewData.pointLandNode = cc.find("mapFrame/pointLandNode", this.node).getComponent("LandNode");
        window.GameViewData.robot = cc.find("mapFrame/robot", this.node).getComponent("RobotNode");
        window.GameViewData.blankLandNode = cc.find("mapFrame/blankLandNode", this.node).getComponent("LandNode");
    },

    update: function (dt) {
        this.refreshTokenView();
    },
    refreshTokenView() {
        if (window.GameViewData.dynamicData.arrChange) {
            window.GameViewData.tokenLayout.node.removeAllChildren(false);

            for (var i = 0; i < window.GameViewData.dynamicData.tokenArr.length; i++) {
                // 通过判断子节点是否处于layout中，如果没在的话，则添加
                if (!window.GameViewData.dynamicData.tokenArr[i].node.isChildOf(window.GameViewData.tokenLayout.node)) {
                    //     window.GameViewData.tokenLayout.node.addChild(window.GameViewData.dynamicData.tokenArr[i].node);
                    //     window.GameViewData.tokenLayout.node.insertChild(window.GameViewData.dynamicData.tokenArr[i].node, i);
                    window.GameViewData.tokenLayout.node.addChild(window.GameViewData.dynamicData.tokenArr[i].node);
                }
            }
            console.log(window.GameViewData.dynamicData.tokenArr);
            window.GameViewData.dynamicData.arrChange = false;
        }

        if (window.GameViewData.dynamicData.func1ArrChange) {
            window.GameViewData.func1Layout.node.removeAllChildren(false);

            for (var i = 0; i < window.GameViewData.dynamicData.func1TokenArr.length; i++) {
                // 通过判断子节点是否处于layout中，如果没在的话，则添加
                if (!window.GameViewData.dynamicData.func1TokenArr[i].node.isChildOf(window.GameViewData.tokenLayout.node)) {
                    window.GameViewData.func1Layout.node.addChild(window.GameViewData.dynamicData.func1TokenArr[i].node);
                }
            }
            console.log(window.GameViewData.dynamicData.func1TokenArr);
            window.GameViewData.dynamicData.func1ArrChange = false;
        }        
        if (window.GameViewData.dynamicData.func2ArrChange) {
            window.GameViewData.func2Layout.node.removeAllChildren(false);

            for (var i = 0; i < window.GameViewData.dynamicData.func2TokenArr.length; i++) {
                // 通过判断子节点是否处于layout中，如果没在的话，则添加
                if (!window.GameViewData.dynamicData.func2TokenArr[i].node.isChildOf(window.GameViewData.tokenLayout.node)) {
                    window.GameViewData.func2Layout.node.addChild(window.GameViewData.dynamicData.func2TokenArr[i].node);
                }
            }
            console.log(window.GameViewData.dynamicData.func2TokenArr);
            window.GameViewData.dynamicData.func2ArrChange = false;
        }

    },

    onEnable: function () {
        this.addListener();
    },
    onDisable: function () {
        this.removeListener();
    },
    addListener: function () {
        this.button1Event =  this.button1.node.on(cc.Node.EventType.TOUCH_END, this.button1Click.bind(this));
       this.button2Event = this.button2.node.on(cc.Node.EventType.TOUCH_END, this.button2Click.bind(this));
       this.button3Event =this.button3.node.on(cc.Node.EventType.TOUCH_END, this.button3Click.bind(this));

        window.EventBus.addListener(window.GameConstant.EventTypeID.OnResultChange, this.OnResultChange.bind(this));
    },

    OnResultChange: function () {
        window.GameViewData.lbResult.string = window.GameViewData.dynamicData.result;
    },

    removeListener: function () {

        this.button1.node.off(cc.Node.EventType.TOUCH_END, this.button3Event);
        this.button2.node.off(cc.Node.EventType.TOUCH_END, this.button2Event);
        this.button1.node.off(cc.Node.EventType.TOUCH_END, this.button3Event);
        window.EventBus.removeListener(window.GameConstant.EventTypeID.OnResultChange);

    },
    button1Click: function () {
        ++window.GameViewData.curStageIndex;
        window.StageManager.loadStage(window.GameViewData.curStageIndex);
    },
    button2Click: function () {
        // console.log("button2 click");
        // console.log(window.cfg);
        window.UIManager.showWindow(window.GameConstant.LayerEnum.UI, window.GameConstant.PrefabNameEnum.GAME_START_WIND);
        window.UIManager.hideWindow(window.GameConstant.PrefabNameEnum.GAME_RUN_WIND);
    },
    button3Click: function () {
        window.TokenManager.operationRobot();
    },

});
